/** 
 * Project Name:kaka 
 * File Name:WxUserInfoServiceImpl.java 
 * Package Name:com.luoran.kaka.service.impl 
 * Date:2018年12月6日下午2:53:55 
 * Copyright (c) 2018, Luoran, Inc. All Rights Reserved.
 * 
 */  
package com.luoran.kaka.service.impl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.luoran.kaka.dao.IWxUserInfoDao;
import com.luoran.kaka.entity.WxUserInfo;
import com.luoran.kaka.service.IWxUserInfoService;

/** 
 * @author ghl 
 * @since JDK 1.8
 * ClassName: WxUserInfoServiceImpl
 * date: 2018年12月6日 下午2:53:55
 *	
 */
@Service
public class WxUserInfoServiceImpl implements IWxUserInfoService {
	
	public static final Logger log = LoggerFactory.getLogger(WxUserInfoServiceImpl.class);
	
	@Autowired
	private IWxUserInfoDao wxUserInfoDao;

	/** 
	 * @see com.luoran.kaka.service.IWxUserInfoService#SyncWxUserInfo() 
	 * @return
	 * @author ghl 2018年12月6日下午2:53:55
	 * SyncWxUserInfo:同步微信数据
	 */
	@Transactional
	@Override
	public void SyncWxUserInfo(WxUserInfo entity) {
		try {
			String openid = entity.getOpenid();
			WxUserInfo wxUserInfo = wxUserInfoDao.getWxUserInfoByOpenId(openid);
			//如果没查到,就保存
			if(null == wxUserInfo) {
				log.info("检测到当前用户为新用户,保存用户基本信息");
				entity.setCreatetime(new Date());
				wxUserInfoDao.insertTemplate(entity);
			}else {
				//如果查到了就做更新操作.
				log.info("检测到当前用户为老用户,更新用户信息");
				wxUserInfo.setNickname(entity.getNickname());//昵称
				wxUserInfo.setSex(entity.getSex());//性别
				wxUserInfo.setCountry(entity.getCountry());//国家
				wxUserInfo.setProvince(entity.getProvince());//省
				wxUserInfo.setCity(entity.getCity());//市
				wxUserInfo.setHeadimgurl(entity.getHeadimgurl());//头像
				wxUserInfo.setPrivilege(entity.getPrivilege());//权限
				wxUserInfo.setUnionid(entity.getUnionid());//
				wxUserInfo.setLastupdatetime(new Date());
				wxUserInfoDao.updateTemplateById(wxUserInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("同步用户微信基本信息失败,详情查看服务器日志.{}",e.getMessage());
			throw new RuntimeException();
		}
	}

}
